package com.yuniarti.konsultasikesehatan;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity {
	
DataParser jsonParser = new DataParser();
	
	EditText txt_username;
	EditText txt_password;
	
	Button btn_login;
	
    private static String cek_login = "http://konsultasi-yakes.web.id/JSON/login.php";
	//private static String cek_login = "http://10.0.2.2/adminweb/JSON/login.php";
 
    // JSON Node names
    private static final String TAG_HASIL = "hasil";
    private static final String TAG_IDMEMBER = "id_member";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login); 
        

        txt_username = (EditText) findViewById(R.id.txt_idmember);
        txt_password = (EditText) findViewById(R.id.txt_password); 

        Button reset = (Button) findViewById(R.id.btn_reset);
        reset.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		txt_username.setText("");
        		txt_password.setText("");
        	}
        });
        
       /* Button daftar = (Button) findViewById(R.id.btn_daftar);
        daftar.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		Intent i = new Intent(getApplicationContext(), DaftarActivity.class);
                startActivity(i);
        	}
        });*/
        
        Button aktifasi = (Button) findViewById(R.id.btn_aktifasi);
        aktifasi.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		Intent i = new Intent(getApplicationContext(), AktifasiActivity.class);
                startActivity(i);
        	}
        });
        
        btn_login = (Button) findViewById(R.id.btn_login);
        btn_login.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View arg0) {
			
				if((txt_username.length()==0) && (txt_password.length()==0)){
        			Toast.makeText(LoginActivity.this, "Id Member dan Password Tidak Boleh Kosong",
        					Toast.LENGTH_LONG).show();
        		}else if(txt_password.length()==0){
        			Toast.makeText(LoginActivity.this, "Password Tidak Boleh Kosong",
        					Toast.LENGTH_LONG).show();
        		}else if(txt_username.length()==0){
        			Toast.makeText(LoginActivity.this, "Id Member Tidak Boleh Kosong",
        					Toast.LENGTH_LONG).show();
        		}else{
				
				String id_member = txt_username.getText().toString();
				String password = txt_password.getText().toString();

				List<NameValuePair> params = new ArrayList<NameValuePair>();

		            params.add(new BasicNameValuePair("id_member", id_member));
		            params.add(new BasicNameValuePair("password", password));
		 
		        JSONObject json = jsonParser.makeHttpRequest(cek_login,"POST", params);
		         
		        
		            try {
		            	
		                int hasil = json.getInt(TAG_HASIL);
		 
		                if (hasil == 1) {
		                  
		                	Toast.makeText(LoginActivity.this, "Login Berhasil YESSS!", Toast.LENGTH_SHORT).show();
		                //	txt_username.setText("Login Berhasil");
		                	
		                    Intent i = new Intent(getApplicationContext(), MenuMemberActivity.class);
		                    i.putExtra(TAG_IDMEMBER, id_member);
		                    startActivity(i);
		                    finish();
		                    
		                }
		                else {
		                	
		                	Toast.makeText(LoginActivity.this, "Login Gagal, " +
		                			"mungkin id member atau password salah atau " +
		                			"anda belum melakukan aktifasi member!", Toast.LENGTH_LONG).show();
		                //	txt_username.setText("Login Gagal");       
		                }
		            	
		                
		            } catch (JSONException e) {
		                e.printStackTrace();
		            }
				
			}}
		});	}
}
